<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Calendar
 * @subpackage Event_Source_Doctrine
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * Zend_Calendar_Event_Source_Doctrine
 *
 * @category   Zend
 * @package    Zend_Calendar
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Calendar_Event_Source_Doctrine extends Zend_Calendar_Event_Source_Abstract implements 
	Zend_Calendar_Event_Source_Interface
{
	/**
	 * @var array Results from Doctrine_Query or Doctrine_Table.
	 */
	protected $_results;
	
	/**
	 * @var string Name of field containing the date the event is to take place.
	 */
	protected $_dateField = 'date';
	
	/**
	 * @var string Format of the _dateField.
	 */
	protected $_dateFormat = Zend_Date::ISO_8601;
	
	public function populateEvents()
	{
		$events = array();
		
		foreach ($this->_results as $row) {
			$day = $this->getDay();
			$month = $this->getMonth();
			$year = $this->getYear();
			
			if (isset($row[$this->_dateField])) {
				$date = $row[$this->_dateField];
				
				if (Zend_Date::isDate($row[$this->_dateField], $this->_dateFormat)) {
					$day = date('d', strtotime($row[$this->_dateField]));
					$month = date('m', strtotime($row[$this->_dateField]));
					$year = date('Y', strtotime($row[$this->_dateField]));
				}
			}
			
			$events[] = new Zend_Calendar_Event($row, $this->getParams(), 
				array('day' => $day, 'month' => $month, 'year' => $year));
		}
		
		$this->_events = $events;
	}
	
	/**
	 * Initialize Doctrine source.
	 */
	public function init()
	{
		$params = $this->getParams();
		if (isset($params['Doctrine_Query'])) {
			$query = $params['Doctrine_Query'];
			
			if (!$query instanceof Doctrine_Query) {
				throw new Zend_Exception('Invalid Doctrine calendar source query.');
			}
		} else if (isset($params['Doctrine_Model'])) {
			$table = $params['Doctrine_Model'];
			if (is_string($table)) {
				$table = Doctrine_Core::getTable($table);
				$this->_results = $table->findAll(Doctrine_Core::HYDRATE_ARRAY);
			} else if ($table instanceof Doctrine_Table) {
				$this->_results = $table->findAll(Doctrine_Core::HYDRATE_ARRAY);
			} else {
				throw new Zend_Exception('Invalid Doctrine calendar source model.');
			}
		} else {
			throw new Zend_Exception(
				'Calendar source Doctrine requires Doctrine_Model or Doctrine_Query parameter.');
		}
		
		if (isset($params['_dateField'])) {
			$this->_dateField = $params['_dateField'];
			unset($params['_dateField']);
		}
		
		if (isset($params['_dateFormat'])) {
			$this->_dateFormat = $params['_dateFormat'];
			unset($params['_dateFormat']);
		}
		
		if (isset($params['Doctrine_Query'])) {
			unset($params['Doctrine_Query']);
		}
		
		if (isset($params['Doctrine_Model'])) {
			unset($params['Doctrine_Model']);
		}
		
		$this->setParams($params);
	}
}